module.exports = options => {
    return async function reqfilter(ctx, next) {

      console.info("正在访问：",ctx.request.path);


      //function_code:path
      adminfunc = [
        {path:'/admin/identity',code:'admin_user'},
        {path:'/admin/role',code:'role_list'},
        // {path:'/admin/role/toquery',code:'role_list'},//将相同前缀层级深的path放数组的后面
        {path:'/admin/dict',code:'dict_list'},
        {path:'/admin/identity',code:'org_list'},
        {path:'/admin/function',code:'function_list'},
        {path:'/admin/news',code:'news_list'},
        {path:'/admin/banner',code:'banner_list'},
        {path:'/admin/street',code:'street_list'},
        {path:'/admin/road',code:'road_list'},
        {path:'/admin/estate',code:'estate_list'},
        {path:'/admin/streetuser',code:'sys_user'},

        // 临时用于测试
        {path:'/api/event',code:'event_list'},
        {path:'/api/trash',code:'waste_list'},
        {path:'/api/menze',code:'menze_list'},
      ]



      if(ctx.app.lodash.startsWith(ctx.request.path,"/admin")){

        // ctx.locals = { userinfo: ctx.session.user_info };
        if(ctx.app.lodash.startsWith(ctx.request.path,"/admin/gen")){

        }else{

          console.info("====="+ctx.app.lodash.isEmpty(ctx.session.user_info))
          if(ctx.app.lodash.isEmpty(ctx.session.user_info)){
            console.info("222222"+ctx.app.lodash.isEmpty(ctx.session.user_info))
            ctx.redirect('/admin/main/toauth')
            return ;
          }else{

            let ps = ctx.app.lodash.filter(adminfunc, function(o) {

              return ctx.app.lodash.startsWith(ctx.request.path,o.path); });

              if(ps.length>0){
                let request_func = ps[ps.length-1]
                var admin_user= ctx.app.lodash.findLast(ctx.session.user_info.authority, function(n) {return n.function_code == request_func.code})
                if(ctx.app.lodash.isEmpty(admin_user)){
                    console.info("没有权限")
                    ctx.status = 401;
                    ctx.redirect('/admin/main/hasnofunc')
                    return ;
                }
              }

          }


        }





      }


      // add by jack @ 20181214
      //function_code:path
      apifunc = [
        // 临时用于测试
        // {path:'/api/event',code:'admin_user'},
      ]

      if(ctx.app.lodash.startsWith(ctx.request.path,"/api")){
          if(ctx.app.lodash.startsWith(ctx.request.path,"/api/user")){

          }else if(ctx.app.lodash.startsWith(ctx.request.path,"/api/mnofilter")){

          }else{
              let token = ctx.request.query.token;
              let user_id;
              if(!ctx.app.lodash.isEmpty(token)){
                user_id = await ctx.app.redis.get(token);
              }
              if(ctx.app.lodash.isEmpty(user_id)){
                  console.log("登录信息过期！");
                  ctx.session.api_user_info=null;
                  ctx.response.body = {
                    errno:100,
                    msg:'登录已过期，请重新登录！'
                  }
                  return ;
              }else{
                if(ctx.app.lodash.isEmpty(ctx.session.api_user_info)){
                    const Op = ctx.app.Sequelize.Op;
                    const sequelize_query_param = {raw:true,type:ctx.app.Sequelize.QueryTypes.SELECT};
                    let sql_user =`SELECT * FROM fd_core_identity WHERE deleted='0' AND id='${user_id}'`;
                    let user = await ctx.model.query(sql_user,sequelize_query_param);
                    let sql_function = `SELECT a.function_code FROM fd_function AS a
                    INNER JOIN fd_role_function AS b ON a.id=b.fd_function_id
                    WHERE a.fun_type='1' AND a.deleted='0' AND b.deleted='0' AND b.fd_role_id='${user[0].role_id}'`;
                    let sql_landmark = `SELECT landmark_id FROM fd_identity_vs_landmark WHERE user_type='${user[0].user_type}' AND deleted='0' AND identity_id='${user[0].id}'`;
                    let landmark_list = [];
                    if(user[0].user_type=='4'){//街镇用户
                        landmark_list = [user[0].street_id];
                    }else{
                        landmark_list = await ctx.model.query(sql_landmark,sequelize_query_param);
                    }
                    let function_list = await ctx.model.query(sql_function,sequelize_query_param);

                    let user_info = {
                        user_id:user[0].id,
                        function_list:function_list,
                        openid:user[0].wx_openid,
                        user_type:user[0].user_type,
                        token:token,
                        landmark_list:landmark_list
                    };
                    ctx.session.api_user_info = user_info;
                }

                  let user_info = ctx.session.api_user_info;
                    let ps = ctx.app.lodash.filter(adminfunc, function(o) {

                      return ctx.app.lodash.startsWith(ctx.request.path,o.path); });

                      if(ps.length>0){
                        let request_func = ps[ps.length-1]
                        var admin_user= ctx.app.lodash.findLast(user_info.function_list, function(n) {return n.function_code == request_func.code})
                        if(ctx.app.lodash.isEmpty(admin_user)){
                          if(ctx.request.path.indexOf("/api/event/")>-1 && user_info.user_type!='4'){
                          }else{
                            console.info("没有权限");
                            ctx.response.body = {
                                errno:1 ,
                                message: '没有权限'
                            }
                            return;
                          }
                        }
                      }

                      }

              }
          }

      await next();
      // 后续中间件执行完成后将响应体转换成 gzip

    };
  };
